#!/usr/bin/env bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

namespace=$1
[ $CCI_SRC ] || export CCI_SRC=/c/compass-ci

umask 002

sed -i "/^NAMESPACE:/cNAMESPACE: $namespace" $CCI_SRC/sparrow/k8s/setup.yaml

for i in setup.yaml info-file remote-hosts
do
	cp $CCI_SRC/sparrow/k8s/$i /etc/compass-ci/$i
done

cat > /etc/profile.d/compass.sh <<'EOF'
export LKP_SRC=/c/lkp-tests
export CCI_SRC=/c/compass-ci
export REPO_SRC=/c/git-repos
export CCI_REPOS=/c
export deploy_k8s=true

export PATH="$PATH:$CCI_SRC/sbin:$LKP_SRC/sbin:$LKP_SRC/bin"
EOF

mkdir -p /root/.config/compass-ci
remote_lkp_pack_server=$(awk '/^remote_lkp_pack_server:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
[[ -f /root/.config/compass-ci/pack.yaml ]] || {
cat > /root/.config/compass-ci/pack.yaml <<EOF
lkp_pack_server: ${HOSTNAME}
lkp_pack_tag_user: root
lkp_rsync_server: ${HOSTNAME}
remote_lkp_pack_server: $remote_lkp_pack_server
EOF

chmod 640 /root/.config/compass-ci/pack.yaml
}

[[ -f /etc/compass-ci/passwd.yaml ]] || {
        passwd=$(< /dev/urandom tr -dc "a-zA-Z0-9" | head -c 10; echo)

        sed -i \
            -e "/key.* ETCD_PASSWORD/ckey ETCD_PASSWORD $passwd" \
            -e "/key.* ES_PASSWORD/ckey ES_PASSWORD $passwd" \
            -e "/key.* ES_SUPER_PASSWORD/ckey ES_SUPER_PASSWORD $passwd" \
            -e "/key.* LOGGING_ES_PASSWORD/ckey LOGGING_ES_PASSWORD $passwd" \
            /etc/compass-ci/info-file

        GITEE_ID=$(awk '/^GITEE_ID:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
        GITEE_PASSWORD=$(awk '/^GITEE_PASSWORD:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
        DOCKER_REGISTRY_USER=$(awk '/^DOCKER_REGISTRY_USER:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
        DOCKER_REGISTRY_PASSWORD=$(awk '/^DOCKER_REGISTRY_PASSWORD:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)

        cat > /etc/compass-ci/passwd.yaml <<-EOF
ES_ROLE: biz-role
ES_USER: biz-user
ES_PASSWORD: $passwd
ES_SUPER_USER: root
ES_SUPER_PASSWORD: $passwd
LOGGING_ES_USER: root
LOGGING_ES_PASSWORD: $passwd
GITEE_ID: $GITEE_ID
GITEE_PASSWORD: $GITEE_PASSWORD
DOCKER_REGISTRY_USER: $DOCKER_REGISTRY_USER
DOCKER_REGISTRY_PASSWORD: $DOCKER_REGISTRY_PASSWORD
ETCD_USER: root
ETCD_PASSWORD: $passwd
REDIS_PASSWD: $passwd
EOF

        chmod 664 /etc/compass-ci/passwd.yaml
}

sh $CCI_SRC/sparrow/k8s/generate-config
[ $namespace == "ems1" ] && sh $CCI_SRC/sparrow/k8s/generate-secrets

configmap_path=$CCI_SRC/container/configmap.yaml
mkdir -p /root/.config/cli/defaults
for i in $(cat $configmap_path | grep ".yaml: |")
do
        file_name=$(echo $i|tr -d ": |")
        [ "$file_name" == "cbs.yaml" ] && sed -n "/$i/,/\-\-\-/p" $configmap_path |sed '1d'|sed '$d' |sed 's/^[ \t]*//g'|tr -s '\n' > /etc/compass-ci/defaults/$file_name
        [ "$file_name" == "service-env.yaml" ] && sed -n "/$i/,/\-\-\-/p" $configmap_path |sed '1d'|sed '$d' |sed 's/^[ \t]*//g'| tr -s '\n' > /etc/compass-ci/service/$file_name
        [ "$file_name" == "config.yaml" ] && sed -n "/$i/,/\-\-\-/p" $configmap_path |sed '1d'|sed '$d' |sed 's/^[ \t]*//g'| tr -s '\n' > /root/.config/cli/defaults/$file_name
done

ruby $(dirname ${BASH_SOURCE[0]})/k8s-env

cat /etc/compass-ci/setup.yaml| grep -E "^ACCOUNT:|^PASSWORD:|^OAUTH_TOKEN_URL:|^OAUTH_REDIRECT_URL:|^PUBLIC_KEY_URL:" >> /root/.config/cli/defaults/config.yaml
cat /etc/compass-ci/setup.yaml| grep -E "^git_branch_|^NAMESPACE:" >> /etc/compass-ci/defaults/cbs.yaml

find /etc/compass-ci/ -type f | xargs chmod 640
chown lkp:lkp -R /etc/compass-ci

[[ -f /etc/compass-ci/defaults/upstream-config ]] || {
cat > /etc/compass-ci/defaults/upstream-config <<EOF
upstreams:
- url: https://gitee.com/thesamename/upstream-repos.git
  location: upstream
  git_repo: u/upstream-repos/upstream-repos
EOF
}

[ "$deploy_k8s" = "true" ] && exit 0

[ -z $server_ip ] && server_ip=$(ip route get 1.2.3.4 | awk '{print $7; exit}')
server_name=$(hostname | cut -f1 -d.)
[[ -f /etc/compass-ci/setup.yaml ]] && {
	lab=$(awk '/^lab:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
}

: ${lab:=nolab}
: ${sched_host:=$server_ip}
: ${sched_port:=3000}
: ${ES_HOST:=$server_ip}
: ${ES_PORT:=9200}
: ${os_http_host:=$server_ip}
: ${OS_HTTP_PORT:=8000}
: ${send_mail_host:=$server_ip}
: ${SEND_MAIL_PORT:=10001}
: ${EMAIL_LIMIT_COUNT:=10}
: ${srv_http_result_host:=$server_ip}
: ${srv_http_os_host:=$server_ip}
: ${srv_http_git_host:=$server_ip}
: ${srv_http_cci_host:=$server_ip}
: ${WEB_BACKEND_HOST:=$server_ip}
: ${WEB_BACKEND_PORT:=10002}
: ${SRV_HTTP_RESULT_PORT:=20007}
: ${SRV_HTTP_INITRD_PORT:=20008}
: ${SRV_HTTP_OS_PORT:=20009}
: ${SRV_HTTP_GIT_PORT:=20010}
: ${SRV_HTTP_CCI_PORT:=20011}
: ${LOGGING_ES_HOST:=$server_ip}
: ${LOGGING_ES_PORT:=9202}
: ${initrd_http_host:=$server_ip}
: ${INITRD_HTTP_PORT:=8800}
: ${assist_result_host:=$server_ip}
: ${ASSIST_RESULT_PORT:=8102}
: ${assistant_host:=$server_ip}
: ${ASSISTANT_PORT:=8101}
: ${result_webdav_host:=$server_ip}
: ${RESULT_WEBDAV_PORT:=3080}
: ${MASTER_FLUENTD_HOST:=$server_ip}
: ${MASTER_FLUENTD_PORT:=24224}
: ${DOCKER_REGISTRY_HOST:=$server_ip}
: ${DOCKER_REGISTRY_PORT:=5001}
: ${LOCAL_SEND_MAIL_HOST:=$server_ip}
: ${LOCAL_SEND_MAIL_PORT:=11311}
: ${LOCAL_ROBOT_EMAIL_ADDRESS:=Crystal_TEAM}
: ${SCHED_HTTPS_HOST:=$server_ip}
: ${SCHED_HTTPS_PORT:=20002}
: ${docker_proxy_host:=$server_ip}
: ${DOCKER_PROXY_PORT:=3128}
: ${jumper_host:=$server_ip}
: ${JUMPER_PORT:=10000}
: ${FORWARD_ISSUER:=/etc/compass-ci/forward-issuer.yaml}
: ${ACCOUNT_ISSUER:=/etc/compass-ci/account-issuer.yaml}
: ${monitor_host:=$server_ip}
: ${MONITOR_PORT:=20001}
: ${FETCH_MAIL_DIR:=/srv/cci/Maildir}
: ${FETCH_MAIL_BOX:=.compass-ci}
: ${GIT_DAEMON_HOST:=$server_ip}
: ${git_mirror_host:=$server_ip}
: ${REDIS_HOST:=$server_ip}
: ${REDIS_PORT:=6379}
: ${MAILDIR:=/srv/cci/Maildir/.compass-ci}
: ${SENT_MAILDIR:=/srv/cci/Maildir/.sent}
: ${NETDATA_PORT:=20013}
: ${WEB_BACKEND_PROTOCOL:=http}
: ${COMPASS_WEB_PROTOCOL:=http}
: ${COMPASS_WEB_PORT:=20030}
: ${OAUTH_CLIENT_ID:=your_client_id_here}
: ${OAUTH_CLIENT_SECRET:=your_client_secret_here}
: ${DAG_HOST:=$server_ip}
: ${DAG_PORT:=20036}
: ${SOURCE_MANAGER_HOST:=$server_ip}
: ${SOURCE_MANAGER_PORT:=20035}
: ${MQ_HOST:=$server_ip}
: ${MQ_PORT:=5672}
: ${GIT_SERVER:=$server_ip}
: ${ETCD_HOST:=$server_ip}
: ${ETCD_PORT:=2379}
: ${JOB_JSON_DIR:=/repositories/new-jobs}
: ${JSON_CONSUME_INTERVAL:=10}
: ${REPO_HOST:=$server_ip}
: ${REPO_PORT:=20029}

mkdir -p /etc/compass-ci/defaults
cat > /etc/compass-ci/defaults/$server_name.yaml <<EOF
SCHED_HOST: $sched_host
SCHED_PORT: $sched_port
lab: "$lab"
lab_id: $lab_id
EOF

jwt_secret=$(uuidgen)

mkdir -p /etc/compass-ci/service
cat > /etc/compass-ci/service/service-env.yaml <<EOF
SCHED_HTTPS_HOST: $SCHED_HTTPS_HOST
SCHED_HTTPS_PORT: $SCHED_HTTPS_PORT
ES_HOST: $ES_HOST
ES_PORT: $ES_PORT
SCHED_HOST: $sched_host
SCHED_PORT: $sched_port
SCHED_NODES:
  - host: $sched_host
    port: 3001
  - host: $sched_host
    port: 3002
  - host: $sched_host
    port: 3003
LKP_SERVER: $server_ip
GIT_SERVER: $GIT_SERVER
OS_HTTP_HOST: $os_http_host
OS_HTTP_PORT: $OS_HTTP_PORT
WEB_BACKEND_HOST: $WEB_BACKEND_HOST
WEB_BACKEND_PORT: $WEB_BACKEND_PORT
SRV_HTTP_RESULT_HOST: $srv_http_result_host
SRV_HTTP_OS_HOST: $srv_http_os_host
SRV_HTTP_GIT_HOST: $srv_http_git_host
SRV_HTTP_CCI_HOST: $srv_http_cci_host
SRV_HTTP_RESULT_PORT: $SRV_HTTP_RESULT_PORT
SRV_HTTP_INITRD_PORT: $SRV_HTTP_INITRD_PORT
SRV_HTTP_OS_PORT: $SRV_HTTP_OS_PORT
SRV_HTTP_GIT_PORT: $SRV_HTTP_GIT_PORT
SRV_HTTP_CCI_PORT: $SRV_HTTP_CCI_PORT
TASKQUEUE_HOST: $sched_host
SEND_MAIL_HOST: $send_mail_host
SEND_MAIL_PORT: $SEND_MAIL_PORT
EMAIL_LIMIT_COUNT: $EMAIL_LIMIT_COUNT
LOGGING_ES_HOST: $LOGGING_ES_HOST
LOGGING_ES_PORT: $LOGGING_ES_PORT
INITRD_HTTP_HOST: $initrd_http_host
INITRD_HTTP_PORT: $INITRD_HTTP_PORT
ASSIST_RESULT_HOST: $assist_result_host
ASSIST_RESULT_PORT: $ASSIST_RESULT_PORT
ASSISTANT_HOST: $assistant_host
ASSISTANT_PORT: $ASSISTANT_PORT
RESULT_WEBDAV_HOST: $result_webdav_host
RESULT_WEBDAV_PORT: $RESULT_WEBDAV_PORT
MASTER_FLUENTD_HOST: $MASTER_FLUENTD_HOST
MASTER_FLUENTD_PORT: $MASTER_FLUENTD_PORT
DOCKER_REGISTRY_HOST: $DOCKER_REGISTRY_HOST
DOCKER_REGISTRY_PORT: $DOCKER_REGISTRY_PORT
LOCAL_SEND_MAIL_HOST: $LOCAL_SEND_MAIL_HOST
LOCAL_SEND_MAIL_PORT: $LOCAL_SEND_MAIL_PORT
MAILDIR: /srv/cci/Maildir/.compass-ci
LOCAL_ROBOT_EMAIL_ADDRESS: $LOCAL_ROBOT_EMAIL_ADDRESS
DOCKER_PROXY_HOST: $docker_proxy_host
DOCKER_PROXY_PORT: $DOCKER_PROXY_PORT
JUMPER_HOST: $jumper_host
JUMPER_PORT: $JUMPER_PORT
ACCOUNT_ISSUER: $ACCOUNT_ISSUER
FORWARD_ISSUER: $FORWARD_ISSUER
MONITOR_HOST: $monitor_host
MONITOR_PORT: $MONITOR_PORT
FETCH_MAIL_DIR: $FETCH_MAIL_DIR
FETCH_MAIL_BOX: $FETCH_MAIL_BOX
GIT_DAEMON_HOST: $GIT_DAEMON_HOST
GIT_MIRROR_HOST: $git_mirror_host
REDIS_HOST: $REDIS_HOST
REDIS_PORT: $REDIS_PORT
MAILDIR: $MAILDIR
SENT_MAILDIR: $SENT_MAILDIR
BASE_RESULT_URL: $WEB_BACKEND_PROTOCOL://$server_ip:$SRV_HTTP_RESULT_PORT
BASE_WEB_BACKEND_URL: $WEB_BACKEND_PROTOCOL://$server_ip:$WEB_BACKEND_PORT/web_backend
OAUTH_CLIENT_ID: $OAUTH_CLIENT_ID
OAUTH_CLIENT_SECRET: $OAUTH_CLIENT_SECRET
OAUTH_REDIRECT_URL: $COMPASS_WEB_PROTOCOL://$server_ip:$COMPASS_WEB_PORT/oauth_authorize
JWT_SECRET: $jwt_secret
DAG_HOST: $DAG_HOST
DAG_PORT: $DAG_PORT
SOURCE_MANAGER_HOST: $SOURCE_MANAGER_HOST
SOURCE_MANAGER_PORT: $SOURCE_MANAGER_PORT
MQ_HOST: $MQ_HOST
MQ_PORT: $MQ_PORT
GIT_SERVER: $GIT_SERVER
ETCD_HOST: $ETCD_HOST
ETCD_PORT: $ETCD_PORT
JOB_JSON_DIR: $JOB_JSON_DIR
JSON_CONSUME_INTERVAL: $JSON_CONSUME_INTERVAL
EOF

mkdir -p /etc/compass-ci/scheduler
cat > /etc/compass-ci/scheduler/local-testbox-env.yaml <<-EOF
LKP_SERVER:
GIT_SERVER:
SRV_HTTP_RESULT_HOST:
SRV_HTTP_RESULT_PORT:
SRV_HTTP_OS_HOST:
SRV_HTTP_OS_PORT:
SRV_HTTP_GIT_HOST:
SRV_HTTP_GIT_PORT:
SRV_HTTP_CCI_HOST:
SRV_HTTP_CCI_PORT:
SEND_MAIL_HOST:
SEND_MAIL_PORT:
LOCAL_SEND_MAIL_HOST:
LOCAL_SEND_MAIL_PORT:
INITRD_HTTP_HOST:
INITRD_HTTP_PORT:
OS_HTTP_HOST:
OS_HTTP_PORT:
ASSIST_RESULT_HOST:
ASSIST_RESULT_PORT:
ASSISTANT_HOST:
ASSISTANT_PORT:
RESULT_WEBDAV_HOST:
RESULT_WEBDAV_PORT:
DOCKER_REGISTRY_HOST:
DOCKER_REGISTRY_PORT:
GIT_MIRROR_HOST:
JUMPER_HOST:
JUMPER_PORT:
FORWARD_ISSUER:
ACCOUNT_ISSUER:
MONITOR_HOST:
MONITOR_PORT:
EOF

mkdir -p /etc/compass-ci/register
cat > /etc/compass-ci/register/register.yaml <<'EOF'
delimiter:
  my_email: delimiter@localhost
  my_name: delimiter
  my_account: delimiter
auto-submit:
  my_email: auto-submit@localhost
  my_name: auto-submit
  my_account: auto-submit
scheduler:
  my_email: compass-ci@localhost
  my_name: compass-ci
  my_account: compass-ci
EOF

source /etc/os-release

path=$(dirname ${BASH_SOURCE[0]})/os/${ID}
[ -x "$path" ] || exit 0
. "$path"
